package med.movyzer.gui;

import java.awt.Component;
import java.awt.HeadlessException;
import java.awt.Image;

import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JFileChooser;

import med.movyzer.gui.MovyzerFileChooserPreviewComponentGT3X;

@SuppressWarnings("serial")
public class MovyzerFileChooser extends JFileChooser implements MouseWheelListener{

	public static final int AUTODETECT = 0;
	public int filetype = 0;
	public static final int GT3X = 1;
	public static final int PSG = 2;
	public MovyzerFileChooserPreviewComponentAUTODETECT a;
	private int sizewatcher = 0;
	JDialog dialog;
	
	public MovyzerFileChooser(int filetype) {
		
		switch (filetype){
		case AUTODETECT: this.setAccessory(a = new MovyzerFileChooserPreviewComponentAUTODETECT(this)); break;
		case GT3X: this.setAccessory(new MovyzerFileChooserPreviewComponentGT3X(this)); break;
		case PSG: this.setAccessory(new MovyzerFileChooserPreviewComponentPSG(this)); break;
		}
	}
	public int getFiletype(){
		return a.getFiletype();
	}
	
	protected JDialog createDialog(Component parent) throws HeadlessException {
		dialog = super.createDialog(parent);
		try {
			Image img = ImageIO.read(this.getClass().getResource("/med/movyzer/gui/gfx/movyzer_mainframe-icon-bw.png"));
			dialog.setIconImage(img);
		} catch (Exception e) {
		}
		dialog.setBounds(75, 75, 700, 400);
		dialog.addMouseWheelListener(this);
		dialog.setResizable(false);
		return dialog;
	}
	
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		resizesMainFrameOfTheChooser(e);	
	}
	
	private void resizesMainFrameOfTheChooser(MouseWheelEvent e) {
		int resize_x = dialog.getToolkit().getScreenSize().width;
		int resize_y = dialog.getToolkit().getScreenSize().height;
		
		if (e.getWheelRotation()>0){ //scroll_down
			if (sizewatcher>0){
				sizewatcher --;
				dialog.setBounds(75, 75, 700, 400);
			}
		}
		if (e.getWheelRotation()<0){ //scroll_up
			if (sizewatcher<1){
				sizewatcher ++;
				dialog.setBounds(0, 0, resize_x, resize_y);
			}
		}
	}
}
